<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); 

// @TODO: Get results only where they are set to active

class Taisan_model extends CI_Model
{
	protected $_table;	    
	/**
	 * Constructor
	 *
	 * @access	public
	 */
	function __construct()
	{
		parent::__construct();
		$this->load->helper('date');
	}
	
	function get_report_tai_san($limit,$start,$type)
	{
		$this->db->select('ts_item.*, ts_hang_muc.name as hang_muc_name');
		$this->db->from('ts_item','ts_hang_muc');
		$this->db->join('ts_hang_muc','ts_hang_muc.id = ts_item.hang_muc_id');
		$this->db->where('ts_item.status',1);
		if($type == 1)
		{
			$this->db->limit($limit, $start);
		}
		$this->db->order_by('ts_item.name','asc');
		$query = $this->db->get();
		$rss =  $query->result_array();
		
		$ret = array();
		foreach($rss as $rs)
		{
			$rs['tengv'] = 'Chưa có phiếu bàn giao';
			$manager = $this->get_current_manage($rs['id']);
			if($manager)
			{
				$rs['tengv'] = $manager[0]['tengv'];
			}
			
			$ret[] = $rs;
		}
		return $ret;
	}
	
	function get_list_taisan($limit, $start)
	{
		$this->db->select('ts_item.*, ts_hang_muc.name as hang_muc_name');
		$this->db->from('ts_item','ts_hang_muc');
		$this->db->join('ts_hang_muc','ts_hang_muc.id = ts_item.hang_muc_id');
		$this->db->limit($limit, $start);
		$this->db->order_by('id','desc');
		$query = $this->db->get();
		return $query->result_array();
	}
	
	function get_current_manage($tai_san_id)
	{
		$this->db->select('giaovien.tengv');
		$this->db->from('ts_chung_tu','ts_chung_tu_item','giaovien');
		$this->db->join('ts_chung_tu_item','ts_chung_tu_item.chung_tu_id = ts_chung_tu.id');
		$this->db->join('giaovien','giaovien.id = ts_chung_tu.manager_id');
		$this->db->where('ts_chung_tu.status',1);
		$this->db->where('ts_chung_tu_item.tai_san_id',$tai_san_id);
		$this->db->order_by('ts_chung_tu_item.id','desc');
		$this->db->limit(1);
		$query = $this->db->get();
		
		if ($query->num_rows() > 0)
		{
			$query = $query->result_array();
			return $query;
		}
		else
		{
			return null;
		}
	}

	
	function get_list_chung_tu($limit, $start)
	{
		$this->db->select('ts_chung_tu.*, giaovien.tengv, giaovien.magv');
		$this->db->from('ts_chung_tu','giaovien');
		$this->db->join('giaovien','giaovien.id = ts_chung_tu.manager_id');
		$this->db->order_by('ts_chung_tu.id','desc');
		$this->db->limit($limit, $start);
		$query = $this->db->get();
		//return $query->result_array();
		//$query = $this->db->get();
		$return = $query->result_array();
		$tmp = array();
		//var_dump($return);exit();
		foreach($return as $item)
		{
			$tongtien = $this->get_total_by_phieutaisan($item['id']);
			$item['total'] = $tongtien->tongtien;
			$tmp[] = $item;
		}
		return $tmp;
		
	}
	
	// submit danh muc
    public function submit_taisan($id=null)
    {
    	$data = array('status' => 1);
    	$this->db->where('id', $id);
    	$this->db->set($data);
    	$this->db->update('ts_item');
    	if($this->db->affected_rows() >= 0)
    	{
    		return TRUE;
    	}
    	else
    	{
    		return FALSE;
    	}
    }
    
    // submit danh muc
    public function submit_chung_tu_ts($id=null)
    {
    	$data = array('status' => 1);
    	$this->db->where('id', $id);
    	$this->db->set($data);
    	$this->db->update('ts_chung_tu');
    	if($this->db->affected_rows() >= 0)
    	{
    		return TRUE;
    	}
    	else
    	{
    		return FALSE;
    	}
    }
	
	public function get_total_by_phieutaisan($id)
	{
		$this->db->select('sum(price) as tongtien');
		$this->db->from('ts_chung_tu_item');
		$this->db->where('chung_tu_id',$id);
		$query = $this->db->get();
		return $query->row();
	}
	
	function get_tai_san($id)
	{
		$this->db->select('*');
		$this->db->from('ts_item');
		$this->db->where('id',$id);
		$query = $this->db->get();
		return $query->row();
	}
	
	function get_chung_tu($id)
	{
		$this->db->select('*');
		$this->db->from('ts_chung_tu');
		$this->db->where('id',$id);
		$query = $this->db->get();
		return $query->row();
	}
	
	function get_chung_tu_item($id)
	{
		$this->db->select('ts_chung_tu_item.tai_san_id, ts_item.code, ts_item.name, ts_item.nguyengia');
		$this->db->from('ts_chung_tu_item','ts_item');
		$this->db->join('ts_item','ts_item.id = ts_chung_tu_item.tai_san_id');
		$this->db->where('ts_chung_tu_item.chung_tu_id',$id);
		$query = $this->db->get();
		return $query->result_array();
	}
	
	function get_all_hang_muc()
	{
		$this->db->select('*');
		$this->db->from('ts_hang_muc');
		$query = $this->db->get();
		return $query->result_array();
	}
	
	function get_all_teacher()
	{
		$this->db->select('id, magv, tengv, user_id');
		$this->db->from('giaovien');
		$this->db->order_by('tengv','asc');
		$query = $this->db->get();
		return $query->result_array();
	}
	
	function get_all_tai_san()
	{
		$this->db->select('*');
		$this->db->where('status',1);
		$this->db->from('ts_item');
		$query = $this->db->get();
		return $query->result_array();
	}
	
	function get_all_xuat_xu()
	{
		$this->db->select('*');
		$this->db->from('vt_xuat_xu');
		$query = $this->db->get();
		return $query->result_array();
	}
	

	public function count_all_tai_san($type)
	{
		$this->db->select('id');
		$this->db->from('ts_item');
		if($type == 1)
		{
			$this->db->where('status',1);
		}
		return $this->db->count_all_results();
	}
	
	public function count_all_chung_tu()
	{
		$this->db->select('id');
		$this->db->from('ts_chung_tu');
		return $this->db->count_all_results();
	}
	
	// Thêm mới Tài sản
	function add_taisan()
	{
		
		$date = str_replace('/', '-', $this->input->post('ngaynhap'));
		$ngaynhap = date('Y-m-d', strtotime($date));
		
		$fields = array(
				'code'			=> $this->input->post('code'),
				'name'			=> $this->input->post('name'),
				'hang_muc_id'	=> $this->input->post('hangmuc'),
				'xuat_xu_id'		=> $this->input->post('xuatxu'),
				'nguyengia'		=> $this->input->post('nguyengia'),
				'thoigiankhauhao'=> $this->input->post('thoigiankhauhao'),
				'ngaynhap'		=> $ngaynhap,
				'tinhtrang'		=> $this->input->post('tinhtrang'),
				'note'			=>	$this->input->post('note'),
				'status'		=>	0,
				'created_date'	=> date('Y-m-d H:i:s'),
		);
		$this->db->set($fields);
		$this->db->insert('ts_item');
		return $this->db->insert_id();
	}
	
	// Thêm mới Phiếu bàn giao Tài sản
	function add_chung_tu($results)
	{
	
		//$date = str_replace('/', '-', $this->input->post('ngaynhap'));
		//$ngaynhap = date('Y-m-d', strtotime($date));
	
		$fields = array(
				'name'			=> $this->input->post('name'),
				'manager_id'	=> $this->input->post('nguoinhan'),
				'note'			=>	$this->input->post('note'),
				'created_date'	=> date('Y-m-d H:i:s'),
				'status'		=> 0,
		);
		$this->db->set($fields);
		$this->db->insert('ts_chung_tu');
		$chung_tu_id =  $this->db->insert_id();
		

		$fields = array(
				'code'			=> 'MPTS-'.$chung_tu_id,
		);
		$this->db->set($fields);
		$this->db->where('id',$chung_tu_id);
		$this->db->update('ts_chung_tu');
		
		if($results)
		{
			foreach ($results as $result)
			{
				$this->add_tai_san_to_chung_tu_item($result, $chung_tu_id);
			}
		}
		
		return $chung_tu_id;
	}
	
	// Sửa Phiếu bàn giao Tài sản
	function edit_chung_tu($idphieu, $results)
	{
	
		$fields = array(
				'name'			=> $this->input->post('name'),
				'manager_id'	=> $this->input->post('nguoinhan'),
				'note'			=>	$this->input->post('note'),
		);
		$this->db->set($fields);
		$this->db->where('id',$idphieu);
		$this->db->update('ts_chung_tu');
		
		$this->remove_tai_san_item_in_bill($idphieu);
		if($results)
		{
			foreach ($results as $result)
			{
				
				$this->add_tai_san_to_chung_tu_item($result, $idphieu);
			}
		}
	
	}
	
	function remove_tai_san_item_in_bill($idphieu)
	{
		$this->db->where('chung_tu_id',$idphieu);
		$this->db->delete('ts_chung_tu_item');
	}
	
	function add_tai_san_to_chung_tu_item($taisan_id, $chung_tu_id)
	{
	
		//$date = str_replace('/', '-', $this->input->post('ngaynhap'));
		//$ngaynhap = date('Y-m-d', strtotime($date));
		$price = $this->input->post('dongia_'.$taisan_id);
		$fields = array(
				'tai_san_id'			=> $taisan_id,
				'chung_tu_id'			=> $chung_tu_id,
				'price'					=> $price,
		);
		$this->db->set($fields);
		$this->db->insert('ts_chung_tu_item');
	}
	
	// Sửa tài sản
	function edit_taisan($id)
	{
		$date = str_replace('/', '-', $this->input->post('ngaynhap'));
		$ngaynhap = date('Y-m-d', strtotime($date));
		
		$fields = array(
				'code'			=> $this->input->post('code'),
				'name'			=> $this->input->post('name'),
				'hang_muc_id'	=> $this->input->post('hangmuc'),
				'xuat_xu_id'	=> $this->input->post('xuatxu'),
				'nguyengia'		=> $this->input->post('nguyengia'),
				'thoigiankhauhao'=> $this->input->post('thoigiankhauhao'),
				'ngaynhap'		=> $ngaynhap,
				'tinhtrang'		=> $this->input->post('tinhtrang'),
				'note'			=>	$this->input->post('note')
		);
		$this->db->set($fields);
		$this->db->where('id',$id);
		$this->db->update('ts_item');
	}
	
	public function search_data($keyword)
	{
		$this->db->select('ts_item.*, ts_hang_muc.name as hang_muc_name');
		$this->db->from('ts_item','ts_hang_muc');
		$this->db->join('ts_hang_muc','ts_hang_muc.id = ts_item.hang_muc_id');
		$this->db->like('ts_item.code', $keyword);
		$this->db->or_like('ts_item.name', $keyword);
		$query = $this->db->get();
			
		if ($query->num_rows() > 0)
		{
			$result = $query->result_array();
			return $result;
		}
		else
		{
			return null;
		}
	}
	
	
	public function check_code()
	{
		$code = $this->input->post('code');
		$query = "
		SELECT code
		FROM ts_item WHERE code ='$code'
		";
		$result = $this->db->query($query);
		if($result->num_rows() >= 1)
		{
		return true;
		}
		else
		{
		return false;
		}
		}
}

/* Location: ./application/models/taisan_model.php */